package activity

import history.*
import code.Status

class Serial {
    String name
    String host//主辦單位
    String staff
    String tel
    String fax
    String email
    String memo
    String code
    String nameEng
    String hostEng
    
    //自動產生欄位
    String url//活動網址產生後即固定，後續可直接出現想要的介紹內容，程式自動算最後一場Events的網址
    String type
    Integer eventCounts = 0
    Date dateStart
    Date dateEnd
    Date regStart
    Date regEnd
    Integer capacityQty = 0
    Integer standbyQty = 0
    Integer registerQty = 0
    Integer registerLeft = 0
    String status
    History history
    SortedSet events
    static hasMany = [events: Event]
    
    static constraints = {
        name(blank:false)
        host(blank:false)
        url(nullable:true)
        memo(nullable:true,size:0..5000)
        tel(nullable:true)
        fax(nullable:true)
        email(nullable:true)
        staff(nullable:true)
        code(nullable:true)
        nameEng(nullable:true)
        hostEng(nullable:true)

        status(nullable:true)
        type(nullable:true)
        eventCounts(nullable:true)
        dateStart(nullable:true)
        dateEnd(nullable:true)
        regStart(nullable:true)
        regEnd(nullable:true)
        capacityQty(nullable:true)
        standbyQty(nullable:true)
        registerQty(nullable:true)
        registerLeft(nullable:true)
        history(nullable:true)
    }
    
    def updateFields(){
        capacityQty = 0
        standbyQty = 0
        registerQty = 0
        eventCounts = events?.size()
        dateStart = null
        dateEnd = null
        regStart = null
        regEnd = null
        def statusList = []

        for (event in events) {
            statusList.add(event.status)
            capacityQty += event.capacityQty ? event.capacityQty : 0
            standbyQty += event.standbyQty ? event.standbyQty : 0
            registerQty += event.registerQty ? event.registerQty : 0
            registerLeft = capacityQty + standbyQty - registerQty
            if (dateStart == null && event.dateStart != null) {
                dateStart = event.dateStart
            } else if (dateStart?.time > event.dateStart?.time) {
                dateStart = event.dateStart
            }
            if (dateEnd == null && event.dateEnd != null) {
                dateEnd = event.dateEnd
            } else if (dateEnd?.time < event.dateEnd?.time) {
                dateEnd = event.dateEnd
            }
            if (regStart == null && event.regStart != null) {
                regStart = event.regStart
            } else if (regStart?.time > event.regStart?.time) {
                regStart = event.regStart
            }
            if (regEnd == null && event.regEnd != null) {
                regEnd = event.regEnd
            } else if (regEnd?.time < event.regEnd?.time) {
                regEnd = event.regEnd
            }
        }
        if(new Date().time < regStart?.time) {//早於報名，尚未開放
            status = "尚未開放"
        } else if (new Date().time > regEnd?.time) {//晚於報名，報名截止
            status = "報名截止"
        } else if (eventCounts == 1) {//單場以場次狀態為主
            status = statusList.get(0)
        } else if (statusList.contains("尚可報名")) {//尚有可報名額
            status = "尚可報名"
        } else if (statusList.contains("候補報名")) {//尚有候補名額
            status = "候補報名"
        } else {
            status = "報名額滿"
        }
//        println "eventCounts:" + eventCounts
//        println "capacityQty:" + capacityQty
//        println "standbyQty:" + standbyQty
//        println "registerQty:" + registerQty
//        println "dateStart:" + dateStart
//        println "dateEnd:" + dateEnd
//        println "regStart:" + regStart
//        println "regEnd:" + regEnd
    }
    
    String toString() {
        name
    }
}
